//	COPYRIGHT (C) 1981 BY BOARD OF TRUSTEES,
//	LELAND STANFORD JUNIOR UNIVERSITY


let PRESUPPOSE() be $(prspps
 /* 

	It is sometimes useful to start a GENOA program presupposing
that the unknown is based upon one of a number of "biologically plausible"
alternative skeleta. There may sometimes be several hundred such
initial candidates (e.g. 200+ biologically plausible sterol side chains).
The ALTERNATIVES command could be used if there were just a small number,
but it is inconvenient with large numbers.

	This function provides a very simple approach to handling a
large number of presupposed alternatives. It must be invoked before
any constraints have been applied. All it does is to take all substructures
defined in one or more user specified files and convert these into
the set of alternative initial cases.

	Most of the work is done in a small separate program called
by GENOA. The functions in this file are concerned with setting up the
data for that program.


*/
static $(
FILENAMES = NIL // List containing names of all the files from which
		// alternative presupposed candidates have been abstracted.
OIN = NIL 	// OIN and OOUT used to hold pointers to I/O streams
OOUT = NIL 	// when reading/writing to some temporary file.
SC2  = NIL	// File pointer for temporary file
SC1  = NIL
QQSTR = "HELP" // Response to ?? entered by user, (eventually this
        	// is intended to be a key into a real "HELP" file.
$)



let PROCESSFILE() BE $(PRF

 static $( NAME = NIL $);

  let TAGCHECK() = valof $(tgchk
   static $( TAGGED = NIL $)

   /* Don't want structures with LNODES or TAGS or POLYATOMS, so in this
  function look at all nodes in substructure to check for these
  properties, returning TRUE if at least one node either TAGGED or
  an LNODE.
  */


   let TAGGEDNODE(CTE) be $(
    if LNP(CTE) then TAGGED:=TRUE;
    if TAGP(CTE) then TAGGED:=TRUE;
    if POLYP(CTE) | (XSTRNUM=CAR(FETCH(CTE.ATS,CTE))) then TAGGED:=TRUE
    $)

   TAGGED:=FALSE;
   MAPC(CTELIST,TAGGEDNODE);
   resultis TAGGED

 $)tgchk


 /* Go through all substructures in the file, check they seem OK
 (CONSTRAINTCHECK + no LNODES + no POLYNAMES etc )
 write out the OK ones,
 maintain list giving sizes of substructures written.
 */

 OIN:=INPUT; 
 INPUT:=FINDFILE("DSK",PROMPTFILENAME,PROMPTFILEEXT)
 SWAPLITEMS()
 FINDSEG(CHUNKSEP,ESHEADSTR);
 $(RPT
	SKIPSEG(ESSEP); 
	LINEIN("");
	if NEXTIS(EOLTYPE) then break;
	NAME:=LOPITEM();
	LINEIN("");
	unless STREQUAL(STROFNUM(LOPITEM()),"SUBSTRUCTURE") do loop;
	READESSTRUC();
	unless CONSTRAINTCHECK(TRUE,NAME,FALSE) do $( CLEAR(); loop $)
	if TAGCHECK() then $( CLEAR(); loop $);
	CONLENGTHS:=CONS(LENGTH(CTELIST),CONLENGTHS)
	OOUT:=OUTPUT; OUTPUT:=SC2;
	PASSESSTRUCOUT()
	OUTPUT:=OOUT
	CLEAR()
 $)RPT REPEAT
 ENDREAD(INPUT);
 SWAPLITEMS()
 INPUT:=OIN
$)PRF


let COMPLETEFILES() be $(cmpltfls
  static $( HCOUNT = NIL $);

 let OUTNOCDR(PR) be
  TEST HSTRNUM=CAR(PR) THEN HCOUNT:=CDR(PR) OR OUTNO(CDR(PR));


 SC1:=CREATEFILE("DSK",SC1FILENAME(),CGEXT);

 OOUT:=OUTPUT; OUTPUT:=SC1;
 WRITERETTOME(THISPARTSTR);

 OUTNOL((ASSOC(HSTRNUM,MOLFORM)=@NULL -> LENGTH(MOLFORM),[LENGTH(MOLFORM)-1]));
 PLIST(MOLFORM,AVOUT,""," "," ","*C*L");
 HCOUNT:=0;
 PLIST(MOLFORM,OUTNOCDR,""," "," ","*C*L");
 OUTNOL(HCOUNT);

 /* Output list of file names, this is used in the problem 'HISTORY' */
 OUTNOL(LENGTH(FILENAMES));
 until FILENAMES=@NULL do $(
	OUTSNUM(CAR(FILENAMES)); SPACES(2);
	FILENAMES:=UNCONS(FILENAMES)
	$)
 NEWLINE(1)

 CONLENGTHS:=DREVERSE(CONLENGTHS);
 OUTNOL(LENGTH(CONLENGTHS));
 PLIST(CONLENGTHS,OUTNO,""," "," ","*C*L");
 NEWLINE(1);
 OIN:=INPUT;
 INPUT:=FINDFILE("DSK",SC2FILENAME(),CGEXT);
 COPYTOEND();
 ENDREAD(INPUT);
 INPUT:=OIN;
 ENDWRITE(OUTPUT);
 OUTPUT:=OOUT
 
$)cmpltfls


let DATAFILE() = valof $(DTF
static  $( FILESTR = NIL $)


TRYPROMPT:
  unless CONDPROMPT("File with substructures : ",
	"(enter the file-name of existing file)",0,QQSTR,STV)
	then resultis FALSE
  FILESTR:=STROFNUM(LITEMS![LPOSN+1]);
  unless UNPACKFILENAME(FILESTR) do
   $( OUTS("Incorrect format for file-name.*C*L"); FLUSHLINE(); goto TRYPROMPT $)

  unless ISSAVEFILE(PROMPTFILENAME,PROMPTFILEEXT) do goto TRYPROMPT;

  FILENAMES:=CONS(LOPITEM(),FILENAMES)
  resultis TRUE

$)DTF
 unless CAR(STRUCSTATUS)=0 do $(
	OUTS("You already have generated some 'CASES'.*C*L");
	OUTS("The 'PRESUPPOSE' command can only be used to create an initial set of CASES.*C*L");
	FLUSHLINE()
	return
	$)

 SC2:=CREATEFILE("DSK",SC2FILENAME(),CGEXT);

 MODMF:=MOLFORM;
 CONLENGTHS:=@NULL;
 FILENAMES:=@NULL;

 while DATAFILE() do PROCESSFILE();
 ENDWRITE(SC2);
 
 /* Check if all a mistake, no files even given for processing. */

 if FILENAMES=@NULL then $( DELETEFILE(SC2FILENAME(),CGEXT); return $);


 COMPLETEFILES();
 test CONLENGTHS=@NULL 
 then OUTS("Nothing in found appropriate to your composition!*C*L")
 or STARTCGPART1(DNDPPN,"PRECAS")
 
 while DELETEFILE(SC1FILENAME(),CGEXT) do;
 UNLIST(CONLENGTHS); CONLENGTHS:=@NULL
 MODMF:=@NULL

$)prspps

